var express = require("express");
var router = express.Router();
var pomelo = require("pomelo");

//validate signature
router.get("/getToken", function(req, res) {
    var msg = req.query;
    pomelo.app.rpc.account.loginRemote.getToken(null, msg, function(data) {
        res.set({
            'content-type': 'application/json; charset=utf-8'
        }).send(JSON.stringify(data));
    });
});

//get one player info by uid
router.get("/uid", function(req, res) {
    var msg = req.query;
    pomelo.app.rpc.account.playerInfoRemote.getPlayerInfoByUid(null, msg, function(data) {
        res.set({
            'content-type': 'application/json; charset=utf-8'
        }).send(JSON.stringify(data));
    });
});

//get one player info by unionid
router.get("/unionid", function(req, res) {
    var msg = req.query;
    pomelo.app.rpc.account.playerInfoRemote.getPlayerInfoByUnionid(null, msg, function(data) {
        res.set({
            'content-type': 'application/json; charset=utf-8'
        }).send(JSON.stringify(data));
    });
});

//get one player info by uid
router.get("/nickname", function(req, res) {
    var msg = req.query;
    pomelo.app.rpc.account.playerInfoRemote.getPlayerInfoByNickname(null, msg, function(data) {
        res.set({
            'content-type': 'application/json; charset=utf-8'
        }).send(JSON.stringify(data));
    });
});

//get all online players info
router.get("/online", function(req, res) {
    var msg = req.query;
    pomelo.app.rpc.account.playerInfoRemote.getOnlinePlayersInfo(null, msg, function(data) {
        res.set({
            'content-type': 'application/json; charset=utf-8'
        }).send(JSON.stringify(data));
    });
});

//get all online players info
router.get("/getAllDisable", function(req, res) {
    var msg = req.query;
    pomelo.app.rpc.account.playerInfoRemote.getAllDisablePlayersInfo(null, msg, function(data) {
        res.set({
            'content-type': 'application/json; charset=utf-8'
        }).send(JSON.stringify(data));
    });
});


//get niuniu gameResult by uid
router.get("/gameResult/niuniu", function(req, res) {
    var msg = req.query;
    pomelo.app.rpc.account.gameResultRemote.getNiuniuGameResult(null, msg, function(data) {
        res.set({
            'content-type': 'application/json; charset=utf-8'
        }).send(JSON.stringify(data));
    });
});

//get zhajinhua gameResult by uid
router.get("/gameResult/zhajinhua", function(req, res) {
    var msg = req.query;
    pomelo.app.rpc.account.gameResultRemote.getZhajinhuaGameResult(null, msg, function(data) {
        res.set({
            'content-type': 'application/json; charset=utf-8'
        }).send(JSON.stringify(data));
    });
});

//get players info by uid
router.post("/registerInfo", function(req, res) {
    var postData = '';
    req.setEncoding("utf8");
    req.on('data', function (chunk) {
        postData += chunk;
    });
    req.on('end', function () {
        postData = JSON.parse(postData);
        pomelo.app.rpc.account.playerInfoRemote.getPlayerRegisterInfos(null, postData, function(data) {
            res.write(JSON.stringify(data));
            res.end();
        });
    });
    // var msg = req.query;
    // pomelo.app.rpc.account.playerInfoRemote.getPlayerRegisterInfos(null, msg, function(data) {
    //     res.set({
    //         'content-type': 'application/json; charset=utf-8'
    //     }).send(JSON.stringify(data));
    // });
});


module.exports = router;